Object Slicing
   HOME

TheInfoList



OR:

In
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
programming, object slicing occurs when an
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ai ...
of a subclass type is copied to an object of superclass type: the superclass copy will not have any of the
member variable In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (''member functions''). In class-based programming languages, these are ...
s or Member functions defined in the subclass. These variables and functions have, in effect, been "sliced off". More subtly, object slicing can likewise occur when an object of a subclass type is copied to an object of the ''same'' type by the superclass's
assignment operator Assignment, assign or The Assignment may refer to: * Homework * Sex assignment * The process of sending National Basketball Association players to its development league; see Computing * Assignment (computer science), a type of modification to ...
, in which case some of the target object's member variables will retain their original values instead of getting copied over from the source object. This issue is not inherently unique to C++, but it does not occur naturally in most other object-oriented languages — not even in C++'s relatives such as D,
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
, and C# — because copying of objects is not a basic operation in those languages. Instead, those languages prefer to manipulate objects via implicit references, such that only copying the ''reference'' is a basic operation. In C++, by contrast, objects are copied automatically whenever a function takes an object argument by value or returns an object by value. Additionally, due to the lack of
garbage collection Waste collection is a part of the process of waste management. It is the transfer of solid waste from the point of use and disposal to the point of treatment or landfill. Waste collection also includes the curbside collection of recyclable m ...
in C++, programs will frequently copy an object whenever the ownership and lifetime of a single shared object would be unclear. For example, inserting an object into a standard library collection (such as a ) actually involves making and inserting a ''copy'' into the collection.


Example

struct A ; struct B : public A ; B &getB() int main()


See also

*
Diamond problem Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or ...


External links


What is the slicing problem in C++?geeksforgeeks.org/object-slicing-in-c/learncpp.com/cpp-tutorial/121-pointers-and-references-to-the-base-class-of-derived-objects/
{{DEFAULTSORT:Object Slicing Object-oriented programming Articles with example C++ code C++